package com.gaoyi.crm.repository;

import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.DbMsg;
import com.gaoyi.crm.db.entity.Category;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.db.entity.Pmn;
import com.gaoyi.crm.db.entity.PmnAttach;
import com.gaoyi.crm.db.entity.PmnItemNo;
import com.gaoyi.crm.util.OKHttpUtil;
import com.gaoyi.crm.util.ThreadManager;
import com.gaoyi.crm.util.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewPmnRepository {

    ItemNoStoreRepository mRepository = new ItemNoStoreRepository();
    PmnRepository mPmn = new PmnRepository();
    PmnAttachRepository mPmnAttach = new PmnAttachRepository();
    CategoryRepository mCategory = new CategoryRepository();
    PmnItemNoRepository mPmnItemNo = new PmnItemNoRepository();

    public void insertPmn(Pmn pmn, List<PmnAttach> pmnAttaches, List<PmnItemNo> pmnItemNos, List<Category> categories, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
            mPmnAttach.insertPmnAttaches(pmnAttaches);
            mPmnItemNo.insertPmnItemNo(pmnItemNos);
            mPmn.insertPmn(pmn);
            if (Utils.isOnline()) { // 离线查询的是本地数据库，不需要再重新插入
                mCategory.insertCategory(categories);
                mRepository.insertItemNoStore(pmn.getItemNos());
            }
            ApiResultData<String> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.INSERT_SUCCESS);
            result.setData(null);
            OKHttpUtil.getInstance().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void insertPmnItemNo(String pmnId, List<ItemNoStore> itemNos, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
            // 删除原先的所有itemNo
            mPmnItemNo.deleteByPmnId(pmnId);
            Date date = new Date();
            // PmnItemNo
            List<PmnItemNo> pmnItemNos = new ArrayList<>();
            // Category
            List<Category> categories = new ArrayList<>();
            List<ItemNoStore> newItemNos = new ArrayList<>();
            for (ItemNoStore itemNo : itemNos) {
                if (itemNo.getCategory() != null) { // 本地查询结果，不需要再重复插入数据
                    itemNo.setSyncedAt(date);
                    itemNo.setSyncedStatus(ApiStatus.DOWNLOAD_SUCCESS);
                    newItemNos.add(itemNo);
                    categories.add(itemNo.getCategory());
                }
                PmnItemNo pmnItemNo = new PmnItemNo();
                pmnItemNo.setId(Utils.getGuid());
                pmnItemNo.setPmnId(pmnId);
                pmnItemNo.setItemNoId(itemNo.getId());
                pmnItemNo.setCreatedAt(date);
                pmnItemNo.setModifyAt(date);
                pmnItemNo.setCreatedId(Utils.getUserId());
                pmnItemNo.setModifyId(Utils.getUserId());
                pmnItemNos.add(pmnItemNo);
            }
            mPmnItemNo.insertPmnItemNo(pmnItemNos);
            if (Utils.isOnline()) { // 离线查询的是本地数据库，不需要再重新插入
                mCategory.insertCategory(categories);
                mRepository.insertItemNoStore(newItemNos);
            }
            ApiResultData<String> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.INSERT_SUCCESS);
            result.setData(null);
            OKHttpUtil.getInstance().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void updatePmn(Pmn bean, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
            String pmnId = bean.getId();
            mPmnAttach.deletePmnAttach(pmnId);
            // PmnAttach
            List<PmnAttach> pmnAttaches = bean.getImages();
            if (pmnAttaches != null && !pmnAttaches.isEmpty()) {
                Date date = new Date();
                for (int i = 0; i < pmnAttaches.size(); i++) {
                    PmnAttach pmnAttach = pmnAttaches.get(i);
                    pmnAttach.setId(Utils.getGuid());
                    pmnAttach.setPmnId(pmnId);
                    pmnAttach.setCreatedAt(date);
                    pmnAttach.setModifyAt(date);
                    pmnAttach.setCreatedId(Utils.getUserId());
                    pmnAttach.setModifyId(Utils.getUserId());
                }
                mPmnAttach.insertPmnAttaches(pmnAttaches);
            }
            mPmn.updatePmn(bean);
            ApiResultData<String> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.UPDATE_SUCCESS);
            result.setData(null);
            OKHttpUtil.getInstance().post(() -> onResultListener.onSuccess(result));
        });
    }
}
